using System;

namespace WP.Commons
{
	public struct PcsLenWid
	{
		public string PcsNum;
		public string LenNum;
		public string WidNum;
	}
	/// <summary>
	/// Summary description for WoodQty.
	/// </summary>
    public class WoodQty
    {
        /// <summary>
        /// analyse string like "12/10/5 14/12/6 20/40/12","10/12 20/10 30/16",....
        ///get Iteminfo like "12/10/5","14/13/6" ,"10/12","20/10",12,10,5,....
        ///Author:LYM  2005.05.19
        /// </summary>
        public string strTextString = "";
        int iSubItemNum = 0;

        #region public WoodQty()
        public WoodQty()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public WoodQty(string strText, StringFormatType stringFomatStype)
        {
            TextString(strText, stringFomatStype);
        }

        #endregion

        #region public void TextString()
        public void TextString(string strText, StringFormatType stringFomatStype)
        {
            strTextString = strText.Trim();
            if (stringFomatStype == StringFormatType.PC_Length_Width)
                iSubItemNum = 3;
            else
                iSubItemNum = 2;
        }
        #endregion

        #region JoinTextString:
        public string JoinTextString(string strText, QtySortBy intsort, bool bNeedJoinInitString, bool bPermitQtyZero)
        {
            string strTxt = "", strReturn = "", strTag = "";
            string[] strTags;
            int i = 0, j = 0, iPos1 = 0, iPos2 = 0;
            bool bBeSpaceChar = false;
            PcsLenWid[] AllItems = null;

            strText = strText.Trim();

            if (bNeedJoinInitString)
            {
                if (strTextString.Trim() != "")
                    strTxt = strTextString + " " + strText;
                else
                    strTxt = strText;
            }
            else
            {
                strTxt = strText;
            }

            while (strTxt.IndexOf("  ") >= 0)
            {
                strTxt = strTxt.Replace("  ", " ");
            }
            strTxt = strTxt.Trim();
            strReturn = strTxt;


            if (strReturn.IndexOf(" ", 0)<0 || strReturn=="" )
            {
                //show only include one item
                if (bNeedJoinInitString) this.strTextString = strReturn;
                return strReturn;
            }
            else
            {
                strTags = strReturn.Split(' ');
                AllItems = new PcsLenWid[strTags.Length];
                for (i = 0; i < strTags.Length; i++)
                {
                    strTag = strTags[i];
                    iPos1 = strTag.IndexOf('/', 0);
                    iPos2 = strTag.IndexOf('/', iPos1 + 1);
                    AllItems[i].PcsNum = strTag.Substring(0, iPos1);
                    if (iPos2 >= 0)
                    {
                        AllItems[i].LenNum = strTag.Substring(iPos1 + 1, iPos2 - iPos1 - 1);
                        AllItems[i].WidNum = strTag.Substring(iPos2 + 1, strTag.Length - iPos2 - 1);
                    }
                    else
                    {
                        AllItems[i].LenNum = strTag.Substring(iPos1 + 1, strTag.Length - iPos1 - 1);
                        AllItems[i].WidNum = "";
                    }

                }


                //join all same len and width item to a item
                for (i = 0; i < AllItems.Length; i++)
                {
                    if (iSubItemNum == 2)
                    {
                        for (j = i + 1; j < AllItems.Length; j++)
                        {
                            if (AllItems[i].LenNum == AllItems[j].LenNum)
                            {
                                if (BaseFunctions.IsNumeric(AllItems[i].PcsNum))
                                {
                                    if (BaseFunctions.IsNumeric(AllItems[j].PcsNum))
                                    {
                                        AllItems[i].PcsNum = (double.Parse(AllItems[i].PcsNum) + double.Parse(AllItems[j].PcsNum)).ToString();
                                        AllItems[j].PcsNum = "";
                                    }
                                }
                                else
                                {
                                    if (BaseFunctions.IsNumeric(AllItems[j].PcsNum))
                                    {
                                        AllItems[i].PcsNum = AllItems[j].PcsNum;
                                        AllItems[j].PcsNum = "";
                                    }
                                }
                            }
                        }
                    }
                    else if (iSubItemNum == 3)
                    {
                        for (j = i + 1; j < AllItems.Length; j++)
                        {
                            if (AllItems[i].LenNum == AllItems[j].LenNum && AllItems[i].WidNum == AllItems[j].WidNum)
                            {
                                if (BaseFunctions.IsNumeric(AllItems[i].PcsNum))
                                {
                                    if (BaseFunctions.IsNumeric(AllItems[j].PcsNum))
                                    {
                                        AllItems[i].PcsNum = (double.Parse(AllItems[i].PcsNum) + double.Parse(AllItems[j].PcsNum)).ToString();
                                        AllItems[j].PcsNum = "";
                                    }
                                }
                                else
                                {
                                    if (BaseFunctions.IsNumeric(AllItems[j].PcsNum))
                                    {
                                        AllItems[i].PcsNum = AllItems[j].PcsNum;
                                        AllItems[j].PcsNum = "";
                                    }
                                }
                            }
                        }
                    }
                }

                //if intSort!= IntNonSort then sort all items by lenth and width
                SoryByLen(ref AllItems, intsort);
                // join all valid item to string
                strReturn = "";
                for (i = 0; i < AllItems.Length; i++)
                {
                    if (iSubItemNum == 2)
                    {
                        if (BaseFunctions.IsNumeric(AllItems[i].PcsNum) && BaseFunctions.IsNumeric(AllItems[i].LenNum))
                        {
                            if (bPermitQtyZero)
                            {
                                if (double.Parse(AllItems[i].LenNum) > 0)
                                {
                                    strReturn = strReturn + " " + AllItems[i].PcsNum.Trim() + "/" + AllItems[i].LenNum.Trim();
                                }
                            }
                            else
                            {
                                if (double.Parse(AllItems[i].PcsNum) != 0 && double.Parse(AllItems[i].LenNum) > 0)
                                {
                                    strReturn = strReturn + " " + AllItems[i].PcsNum.Trim() + "/" + AllItems[i].LenNum.Trim();
                                }
                            }
                        }
                    }
                    else if (iSubItemNum == 3)
                    {
                        if (BaseFunctions.IsNumeric(AllItems[i].PcsNum) && BaseFunctions.IsNumeric(AllItems[i].LenNum) && BaseFunctions.IsNumeric(AllItems[i].WidNum))
                        {
                            if (bPermitQtyZero)
                            {
                                if (double.Parse(AllItems[i].LenNum) > 0 && double.Parse(AllItems[i].WidNum) > 0)
                                {
                                    strReturn = strReturn + " " + AllItems[i].PcsNum.Trim() + "/" + AllItems[i].LenNum.Trim() + "/" + AllItems[i].WidNum.Trim();
                                }
                            }
                            else
                            {
                                if (double.Parse(AllItems[i].PcsNum) != 0 && double.Parse(AllItems[i].LenNum) > 0 && double.Parse(AllItems[i].WidNum) > 0)
                                {
                                    strReturn = strReturn + " " + AllItems[i].PcsNum.Trim() + "/" + AllItems[i].LenNum.Trim() + "/" + AllItems[i].WidNum.Trim();
                                }
                            }
                        }
                    }
                }
            }
            strReturn = strReturn.Trim();
            if (bNeedJoinInitString) this.strTextString = strReturn;
            return strReturn;
        }
        public string JoinTextString(string strText, QtySortBy intsort, bool bNeedJoinInitString)
        {
            return JoinTextString(strText, intsort, bNeedJoinInitString, false);
        }
        public string JoinTextString(string strText, QtySortBy intsort)
        {
            return JoinTextString(strText, intsort, true, false);
        }
        public string JoinTextString(string strText)
        {
            return JoinTextString(strText, QtySortBy.IntNonSort, true, false);
        }
        #endregion


        public int ItemsCount()
        {
            int i = 0, j = 0;
            for (i = 0; i < strTextString.Length - 1; i++)
            {
                if (strTextString.Substring(i, 1) == "/") j++;
            }
            if (iSubItemNum == 3)
            {
                j = j / 2;
            }
            return j;
        }

        public double TotalNum()
        {
            int i = 0;
            double iTotal = 0;
            for (i = 0; i < this.ItemsCount(); i++)
            {
                if (iSubItemNum == 2)
                {
                    iTotal += double.Parse(this.ItemInfo(i, ItemInfoOpt.OnlyPcs)) * double.Parse(this.ItemInfo(i, ItemInfoOpt.OnlyLen));
                }
                else
                {
                    iTotal += double.Parse(this.ItemInfo(i, ItemInfoOpt.OnlyPcs)) * double.Parse(this.ItemInfo(i, ItemInfoOpt.OnlyLen)) * double.Parse(this.ItemInfo(i, ItemInfoOpt.OnlyWidth));
                }
            }
            return iTotal;
        }

        public double TotalLength()
        {
            int i = 0;
            double iTotal = 0;
            for (i = 0; i < this.ItemsCount(); i++)
            {
                iTotal += double.Parse(this.ItemInfo(i, ItemInfoOpt.OnlyPcs)) * double.Parse(this.ItemInfo(i, ItemInfoOpt.OnlyLen));
            }
            return iTotal;

        }
        #region SoryByLen:
        private void SoryByLen(ref PcsLenWid[] AllItems, QtySortBy intsort)
        {
            PcsLenWid udtItem;
            int i = 0, j = 0;

            switch (intsort)
            {
                case QtySortBy.IntNonSort:
                    return;
                case QtySortBy.IntAscent:
                    for (i = 0; i < AllItems.Length; i++)
                    {
                        if (iSubItemNum == 2)
                        {
                            if (BaseFunctions.IsInt(AllItems[i].PcsNum) && BaseFunctions.IsInt(AllItems[i].LenNum))
                            {
                                if (int.Parse(AllItems[i].LenNum) > 0)
                                {
                                    udtItem.PcsNum = AllItems[i].PcsNum;
                                    udtItem.LenNum = AllItems[i].LenNum;
                                    udtItem.WidNum = AllItems[i].WidNum;
                                    for (j = i + 1; j < AllItems.Length; j++)
                                    {
                                        if (BaseFunctions.IsInt(AllItems[j].PcsNum) && BaseFunctions.IsInt(AllItems[j].LenNum))
                                        {
                                            if (int.Parse(AllItems[j].LenNum) > 0)
                                            {
                                                if (int.Parse(udtItem.LenNum) > int.Parse(AllItems[j].LenNum))
                                                {
                                                    AllItems[i].PcsNum = AllItems[j].PcsNum;
                                                    AllItems[i].LenNum = AllItems[j].LenNum;
                                                    AllItems[i].WidNum = AllItems[j].WidNum;
                                                    AllItems[j].PcsNum = udtItem.PcsNum;
                                                    AllItems[j].LenNum = udtItem.LenNum;
                                                    AllItems[j].WidNum = udtItem.WidNum;
                                                    udtItem.PcsNum = AllItems[i].PcsNum;
                                                    udtItem.LenNum = AllItems[i].LenNum;
                                                    udtItem.WidNum = AllItems[i].WidNum;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (iSubItemNum == 3)
                        {
                            if (BaseFunctions.IsInt(AllItems[i].PcsNum) && BaseFunctions.IsInt(AllItems[i].LenNum) && BaseFunctions.IsInt(AllItems[i].WidNum))
                            {
                                if (int.Parse(AllItems[i].LenNum) > 0 && int.Parse(AllItems[i].WidNum) > 0)
                                {
                                    udtItem.PcsNum = AllItems[i].PcsNum;
                                    udtItem.LenNum = AllItems[i].LenNum;
                                    udtItem.WidNum = AllItems[i].WidNum;
                                    for (j = i + 1; j < AllItems.Length; j++)
                                    {
                                        if (BaseFunctions.IsInt(AllItems[j].PcsNum) && BaseFunctions.IsInt(AllItems[j].LenNum))
                                        {
                                            if (int.Parse(AllItems[j].LenNum) > 0)
                                            {
                                                if (int.Parse(udtItem.LenNum) > int.Parse(AllItems[j].LenNum) ||
                                                    (int.Parse(udtItem.LenNum) == int.Parse(AllItems[j].LenNum) && int.Parse(udtItem.WidNum) > int.Parse(AllItems[j].WidNum)))
                                                {
                                                    AllItems[i].PcsNum = AllItems[j].PcsNum;
                                                    AllItems[i].LenNum = AllItems[j].LenNum;
                                                    AllItems[i].WidNum = AllItems[j].WidNum;
                                                    AllItems[j].PcsNum = udtItem.PcsNum;
                                                    AllItems[j].LenNum = udtItem.LenNum;
                                                    AllItems[j].WidNum = udtItem.WidNum;
                                                    udtItem.PcsNum = AllItems[i].PcsNum;
                                                    udtItem.LenNum = AllItems[i].LenNum;
                                                    udtItem.WidNum = AllItems[i].WidNum;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case QtySortBy.IntDescend:
                    for (i = 0; i < AllItems.Length; i++)
                    {
                        if (iSubItemNum == 2)
                        {
                            if (BaseFunctions.IsInt(AllItems[i].PcsNum) && BaseFunctions.IsInt(AllItems[i].LenNum))
                            {
                                if (int.Parse(AllItems[i].PcsNum) > 0 && int.Parse(AllItems[i].LenNum) > 0)
                                {
                                    udtItem.PcsNum = AllItems[i].PcsNum;
                                    udtItem.LenNum = AllItems[i].LenNum;
                                    udtItem.WidNum = AllItems[i].WidNum;
                                    for (j = i + 1; j < AllItems.Length; j++)
                                    {
                                        if (BaseFunctions.IsInt(AllItems[j].PcsNum) && BaseFunctions.IsInt(AllItems[j].LenNum))
                                        {
                                            if (int.Parse(AllItems[j].LenNum) > 0)
                                            {
                                                if (int.Parse(udtItem.LenNum) < int.Parse(AllItems[j].LenNum))
                                                {
                                                    AllItems[i].PcsNum = AllItems[j].PcsNum;
                                                    AllItems[i].LenNum = AllItems[j].LenNum;
                                                    AllItems[i].WidNum = AllItems[j].WidNum;
                                                    AllItems[j].PcsNum = udtItem.PcsNum;
                                                    AllItems[j].LenNum = udtItem.LenNum;
                                                    AllItems[j].WidNum = udtItem.WidNum;
                                                    udtItem.PcsNum = AllItems[i].PcsNum;
                                                    udtItem.LenNum = AllItems[i].LenNum;
                                                    udtItem.WidNum = AllItems[i].WidNum;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (iSubItemNum == 3)
                        {
                            if (BaseFunctions.IsInt(AllItems[i].PcsNum) && BaseFunctions.IsInt(AllItems[i].LenNum) && BaseFunctions.IsInt(AllItems[i].WidNum))
                            {
                                if (int.Parse(AllItems[i].PcsNum) > 0 && int.Parse(AllItems[i].LenNum) > 0 && int.Parse(AllItems[i].WidNum) > 0)
                                {
                                    udtItem.PcsNum = AllItems[i].PcsNum;
                                    udtItem.LenNum = AllItems[i].LenNum;
                                    udtItem.WidNum = AllItems[i].WidNum;
                                    for (j = i + 1; j < AllItems.Length; j++)
                                    {
                                        if (BaseFunctions.IsInt(AllItems[j].PcsNum) && BaseFunctions.IsInt(AllItems[j].LenNum))
                                        {
                                            if (int.Parse(AllItems[j].LenNum) > 0)
                                            {
                                                if (int.Parse(udtItem.LenNum) < int.Parse(AllItems[j].LenNum) ||
                                                    (int.Parse(udtItem.LenNum) == int.Parse(AllItems[j].LenNum) && int.Parse(udtItem.WidNum) < int.Parse(AllItems[j].WidNum)))
                                                {
                                                    AllItems[i].PcsNum = AllItems[j].PcsNum;
                                                    AllItems[i].LenNum = AllItems[j].LenNum;
                                                    AllItems[i].WidNum = AllItems[j].WidNum;
                                                    AllItems[j].PcsNum = udtItem.PcsNum;
                                                    AllItems[j].LenNum = udtItem.LenNum;
                                                    AllItems[j].WidNum = udtItem.WidNum;
                                                    udtItem.PcsNum = AllItems[i].PcsNum;
                                                    udtItem.LenNum = AllItems[i].LenNum;
                                                    udtItem.WidNum = AllItems[i].WidNum;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
            }
        }
        #endregion

        #region ItemInfo
        public string ItemInfo(int Index, ItemInfoOpt WhichSubItem)
        {
            int i = 0, j = 0;
            string strItemValue = "", strItemInfo = "";
            int BarFindNumbers = 0, StandardBarFindNumbers = 0;

            if (iSubItemNum == 2)
                StandardBarFindNumbers = 1;
            else if (iSubItemNum == 3)
                StandardBarFindNumbers = 2;

            for (j = 0; j < strTextString.Length; j++)
            {
                if (strTextString.Substring(j, 1) == "/")
                    BarFindNumbers++;

                strItemValue += strTextString.Substring(j, 1);
                strItemValue = strItemValue.Trim();
                if ((BarFindNumbers == StandardBarFindNumbers && strTextString.Substring(j, 1) == " ") || j == strTextString.Length - 1)
                {

                    if (i == Index)
                    {
                        strItemInfo = GetItemInfo(strItemValue, WhichSubItem);
                        return strItemInfo;
                    }
                    strItemValue = "";
                    BarFindNumbers = 0;
                    i++;
                }
            }
            if (WhichSubItem != ItemInfoOpt.WholeItem && strItemInfo == "") strItemInfo = "0";
            return strItemInfo;
        }
        #endregion

        #region GetItemInfo
        public static string GetItemInfo(string strItemValue, ItemInfoOpt WhichSubItem)
        {
            int iPos1 = 0, iPos2 = 0;
            string strItemInfo = "";
            int iNum = 2;
            string[] arrSplit = strItemValue.Split('/');
            if (arrSplit.Length == 3) iNum = 3;

            if (iNum == 2)
            {
                iPos1 = strItemValue.IndexOf("/");
                if (WhichSubItem == ItemInfoOpt.WholeItem)
                {
                    strItemInfo = strItemValue.Trim();
                }
                else if (WhichSubItem == ItemInfoOpt.OnlyPcs)
                {
                    if (iPos1 > 0)
                    {
                        strItemInfo = strItemValue.Substring(0, iPos1);
                        strItemInfo =  CvtFunc.ToInt(strItemInfo).ToString() ;
                    }
                    else
                    {
                        strItemInfo = "0";
                    }
                }
                else if (WhichSubItem == ItemInfoOpt.OnlyLen)
                {
                    if (iPos1 > 0)
                    {
                        strItemInfo = strItemValue.Substring(iPos1 + 1);
                        strItemInfo = (BaseFunctions.IsDouble(strItemInfo) ? strItemInfo : "0");
                    }
                    else
                    {
                        strItemInfo = "0";
                    }
                }
                else if (WhichSubItem == ItemInfoOpt.OnlyWidth)
                {
                    strItemInfo = "0";
                }

            }
            else if (iNum == 3)
            {
                iPos1 = strItemValue.IndexOf("/");
                iPos2 = strItemValue.IndexOf("/", iPos1 + 1);

                if (WhichSubItem == ItemInfoOpt.WholeItem)
                {
                    strItemInfo = strItemValue.Trim();
                }
                else if (WhichSubItem == ItemInfoOpt.OnlyPcs)
                {
                    if (iPos1 > 0)
                    {
                        strItemInfo = strItemValue.Substring(0, iPos1);
                        strItemInfo = CvtFunc.ToInt(strItemInfo).ToString() ;
                    }
                    else
                    {
                        strItemInfo = "0";
                    }
                }
                else if (WhichSubItem == ItemInfoOpt.OnlyLen)
                {
                    if (iPos1 > 0)
                    {
                        if (iPos2 > 0)
                        {
                            strItemInfo = strItemValue.Substring(iPos1 + 1, iPos2 - iPos1 - 1);
                            strItemInfo = CvtFunc.ToDouble(strItemInfo).ToString().Trim()  ;
                        }
                        else
                        {
                            strItemInfo = "0";
                        }
                    }
                    else
                    {
                        strItemInfo = "0";
                    }
                }
                else if (WhichSubItem == ItemInfoOpt.OnlyWidth)
                {
                    if (iPos1 > 0)
                    {
                        if (iPos2 > 0)
                        {
                            strItemInfo = strItemValue.Substring(iPos2 + 1);

                            strItemInfo =CvtFunc.ToDouble(strItemInfo).ToString() ;
                        }
                        else
                        {
                            strItemInfo = "0";
                        }
                    }
                    else
                    {
                        strItemInfo = "0";
                    }
                }
            }
            if (WhichSubItem != ItemInfoOpt.WholeItem && strItemInfo == "") strItemInfo = "0";
            return strItemInfo;
        }
        #endregion
        /// <summary>
        /// iOperType  =1 ----/ 2-----*
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="iOperNum"></param>
        /// <param name="iOperType"></param>
        /// <returns></returns>
        public static string StringOperation(string strText, int iOperNum, int iOperType)
        {
            // 1----/   
            // 2----* 

            string strSource = "";
            int iPcs = 0;
            int i = 0;
            string strPc = "";
            string strResult = "";

            if (iOperNum == 1 || strText.Trim() == "" || iOperType == 0) return strText.Trim();
            strSource = strText.Trim();
            while (strSource.IndexOf("  ") >= 0)
            {
                strSource = strSource.Replace("  ", " ");
            }
            string[] arrItems = strSource.Split(' ');
            for (i = 0; i < arrItems.Length; i++)
            {
                if (iOperType == 1)
                {
                    if (arrItems[i].IndexOf('/') > 0)
                    {
                        strPc = BaseFunctions.FormatNumeric(arrItems[i].Substring(0, arrItems[i].IndexOf('/')), "n.0");
                        iPcs = int.Parse(strPc);
                        if (iPcs / iOperNum == 0)
                            arrItems[i] = "";
                        else
                            arrItems[i] = Convert.ToString(iPcs / iOperNum).Trim() + arrItems[i].Substring(arrItems[i].IndexOf('/'));
                    }
                    else
                    {
                        arrItems[i] = "";
                    }

                }
                else
                {
                    if (arrItems[i].IndexOf('/') > 0)
                    {
                        strPc = BaseFunctions.FormatNumeric(arrItems[i].Substring(0, arrItems[i].IndexOf('/')), "n.0");
                        iPcs = int.Parse(strPc);
                        if (iPcs * iOperNum == 0)
                            arrItems[i] = "";
                        else
                            arrItems[i] = Convert.ToString(iPcs * iOperNum).Trim() + arrItems[i].Substring(arrItems[i].IndexOf('/'));
                    }
                    else
                    {
                        arrItems[i] = "";
                    }

                }
                if (arrItems[i] != "") strResult += " " + arrItems[i].Trim();
            }
            return strResult.Trim();
        }
    }
}



